package com.situ.hospital.patient.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.hospital.patient.dao.IPatientDao;
import com.situ.hospital.patient.model.PatientModel;

import util.IsEmpty;
import util.JDBCUtil;

public class PatientDaoImpl implements IPatientDao {
	@Override
	public Integer insert(PatientModel patient) {
		String sql = "insert into patient(patient_name,patient_sex,room_id,doctor_id) values(?,?,?,?)";
		return JDBCUtil.dml(sql, patient.getPatientName(), patient.getPatientSex(), patient.getRoomId(),
				patient.getDoctorId());
	}

	@Override
	public PatientModel selectModel(PatientModel patient) throws SQLException {
		String sql = "select * from patient where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, patient, 2);
		List<PatientModel> backlist = JDBCUtil.dql(sql, list, PatientModel.class, patient.mapGet());
		
		if (backlist == null || backlist.size() == 0) {
			return null;
		} else {
			return backlist.get(0);
		}
	}

	@Override
	public List<PatientModel> selectAll(PatientModel patient) throws SQLException {
		String sql = "select * from patient where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, patient, 2);
		System.out.println(sql);
		return JDBCUtil.dql(sql, list, PatientModel.class, patient.mapGet());
	}

	@Override
	public Integer deleteByName(PatientModel patient) throws SQLException {
		String sql = "delete from patient where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, patient, 2);
		return JDBCUtil.dml(sql, list);
	}

	@Override
	public Integer updateByName(PatientModel patient) throws SQLException {
		String sql = "update patient set ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, patient, 3);
		System.out.println(sql);
		return JDBCUtil.dml(sql, list);
	}
	
	@Override
	public Integer selectCount(PatientModel patient) throws SQLException {
		String sql = "select count(1) from patient where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, patient, 2);
		return JDBCUtil.selectCount(sql, list);

	}

	/**
	 * @param sql
	 * @param list
	 * @param department
	 * @param n          1为insert 2为select 2为delete 3为update
	 * @return
	 */
	private String setSql(String sql, List<Object> list, PatientModel patient, int m) {
		int n = 0;

		if (patient.getId() != null && patient.getId() == 1) {
			sql += "patient_name like ? and patient_sex like ?";
			
			list.add(patient.getPatientName());
			list.add(patient.getPatientSex());
			if (patient.isPageOn()) {
				sql += " limit ?,?";
				list.add(patient.getRowStart());
				list.add(patient.getPageLimit());
			}
			return sql;
		}
		String name=patient.getPatientName();
		if (!IsEmpty.isEmpty(name) && m != 3) {
			if (n == 1) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "patient_name=?";
			list.add(name);
			n = m;
		}
		
		String sex=patient.getPatientSex();
		if (!IsEmpty.isEmpty(sex)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "patient_sex=?";
			list.add(sex);
			n = m;
		}
		
		Integer roomid=patient.getRoomId();
		if (!IsEmpty.isEmpty(roomid)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "room_id=?";
			list.add(roomid);
			n = m;
		}
		
		Integer doctorid=patient.getDoctorId();
		if (!IsEmpty.isEmpty(doctorid)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "doctor_id=?";
			list.add(doctorid);
			n = m;
		}
		
		if (m == 3) {
			sql += "where patient_name=?";
			list.add(name);
		}

		return sql;
	}
}
